package com.weilele.leisure.time.ui.module.search

import android.os.Bundle
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import com.weilele.base.library.BaseBindingFragment
import com.weilele.base.library.FragmentContainerActivity
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.VideoListBean
import com.weilele.leisure.time.databinding.ActivitySearchBinding
import com.weilele.mvvm.base.helper.annotation.MvvmActivityViewModel
import com.weilele.mvvm.base.livedata.*
import com.weilele.mvvm.utils.activity.*
import com.weilele.mvvm.view.SimpleSwitchView
import com.weilele.leisure.time.bean.db.HistorySearchBean

class SearchActivity : BaseBindingFragment<ActivitySearchBinding>() {
    companion object {
        fun start(activity: AppCompatActivity) {
            FragmentContainerActivity.start<SearchActivity>(activity)
        }
    }

    @MvvmActivityViewModel
    private lateinit var searchViewModel: SearchViewModel

    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(
            searchViewModel.clickHistorySearchData wrapSuccessObserver ::onClickSearch,
            searchViewModel.videoData wrapObserver ::onSearVideo,
        )
    }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        mBinding.etKeyword.onSearch {
            startSearch(it)
        }
        mBinding.toolbar.setNavigationOnClickListener {
            appCompatActivity?.finishAfterTransition()
        }
        mBinding.toolbar.setOnMenuItemClickListener {
            when (it.itemId) {
                R.id.search -> {
                    startSearch(mBinding.etKeyword.text.toString())
                }
                else -> {
                }
            }
            return@setOnMenuItemClickListener true
        }
    }


    private fun onClickSearch(bean: HistorySearchBean) {
        val keyword = bean.keyword ?: return
        mBinding.etKeyword.setText(keyword)
        mBinding.etKeyword.setSelection(keyword.count())
        startSearch(keyword)
    }

    override fun isNeedSwitchView(): Boolean {
        return true
    }

    /**
     * 开始搜索
     */
    private fun startSearch(keywords: String?) {
        appCompatActivity?.hiddenKeyboard()
        searchViewModel.search(keywords)
    }

    /**
     * 视频搜索回调
     */
    private fun onSearVideo(value: StatusData<VideoListBean>?) {
        when (value?.status) {
            StatusData.Error -> {
                showContentView()
            }
            StatusData.Running -> {
                if (!mBinding.fgVideoList.isVisible()) {
                    refreshSearchVideo(null)
                    mBinding.fgVideoList.visible()
                }
                showLoadingView(SimpleSwitchView.LoadingInfo())
            }
            StatusData.Success -> {
                showContentView()
                refreshSearchVideo(value.data?.rows)
            }
        }
    }

    private fun refreshSearchVideo(datas: MutableList<VideoListBean.RowsDTO>?) {
        mBinding.fgVideoList.getBindFragment<VideoListFragment>(this)
            ?.refresh(datas)
    }

    override fun onBackPressed() = object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            if (mBinding.fgVideoList.isVisible()) {
                mBinding.fgVideoList.invisible()
                mBinding.fgVideoList.getBindFragment<VideoListFragment>(this@SearchActivity)
                    ?.refresh(null)
            } else {
                appCompatActivity?.finishAfterTransition()
            }
        }
    }
}